<template>
	<cl-dialog
		title="上传素材"
		width="400px"
		:visible.sync="visible"
		@close="() => (visible = false)"
		:props="{
			top: '5vh',
			'append-to-body': true,
			closeOnClickModal: false
		}"
	>
		<el-form ref="form" label-position="top" :model="form" :rules="rules" :disabled="saving">
			<el-form-item label="选择微信号" prop="model_ids">
				<search-select style="width: 100%" ref="searchSelect" v-model="form.model_ids">
				</search-select>
			</el-form-item>
			<el-form-item
				label="是否把公众号的推广人和广告账户同步到加微方式上"
				prop="sync_info_flag"
			>
				<cl-select style="width: 100%" v-model="form.sync_info_flag">
					<el-option label="是" :value="1" />
					<el-option label="否" :value="0" />
				</cl-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="() => (visible = false)">关闭</el-button>
			<el-button type="primary" :loading="saving" @click="uploadSubmit">确定</el-button>
		</template>
	</cl-dialog>
</template>
<script>
import SearchSelect from "./search-select";
import axios from "axios";
export default {
	components: { SearchSelect },
	data() {
		return {
			visible: false,
			saving: false,
			form: {
				id: "",
				model_ids: "",
				sync_info_flag: 0,
				operator_id: ""
			},
			rules: {
				model_ids: [{ required: true, message: "请选择选择微信号" }],
				sync_info_flag: [{ required: true, message: "请选择选择是或者否" }]
			},
			bra_id: ""
		};
	},
	methods: {
		open({ id, bra_id }, biz_id) {
			this.bra_id = bra_id;
			this.visible = true;
			this.saving = false;
			this.form.id = id;
			this.$nextTick(() => {
				this.$refs.searchSelect.init({
					biz_id: biz_id, //事业部id
					showpage: 1, //是否分页
					id_join_type: ["4", "5"] //只搜索二维吗和企业微信吗
				});
			});
			this.form.model_ids = "";
			this.form.sync_info_flag = 0;
		},
		uploadSubmit() {
			this.saving = true;
			this.$refs.form.validate(async (valid, errors) => {
				if (!valid) {
					this.saving = false;
					return;
				}
				let form = JSON.parse(JSON.stringify(this.form));
				form.model_ids = [this.form.model_ids];
				// console.log(form)
				this.$service.wx
					.syncMaterial(form)
					.then(res => {
						this.saving = false;
						this.$message.success("操作成功");
						this.visible = false;
					})
					.catch(res => {
						this.saving = false;
					});
			});
		}
	}
};
</script>
